package com.cskaoyan.service;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Component("userServiceProxy")
public class UserServiceProxyFactoryBean implements FactoryBean<UserSerivce> {

    @Autowired
    @Qualifier("userServiceImpl")
    UserSerivce userSerivce;

    @Override
    public UserSerivce getObject() throws Exception {
        UserSerivce proxy = (UserSerivce) Enhancer.create(userSerivce.getClass(), new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                System.out.println("aaaaa");
                Object invoke = method.invoke(userSerivce, objects);
                System.out.println("bbbbb");
                return invoke;
            }
        });
        return proxy;
    }

    @Override
    public Class<?> getObjectType() {
        return UserSerivce.class;
    }
}
